﻿using Rapid.Entity.Basic;
using Rapid.MvcExtension.Controllers;
using Rapid.Core.Aop;
using Rapid.Utility.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Rapid.MvcApplication.Areas.Manage.Controllers
{
    public class RoleController : ManageController
    {
        public ActionResult List()
        {
            return View();
        }

        public ActionResult Page(string id)
        {
            BasicRole model;

            if (string.IsNullOrEmpty(id))
            {
                model = new BasicRole();

                model.IsEnabled = true;
            }
            else { model = ServiceFactory.BasicRole.Get(id); }

            return View(model);
        }

        public ActionResult Purview()
        {
            return View();
        }

        public string Grid(int rows, int page)
        {
            int count = 0;

            var list = ServiceFactory.BasicRole.Grid(rows, page, out count);

            return JsonHelper.Grid(list, count);
        }

        public string AllGrid()
        {
            return JsonHelper.Serialize(ServiceFactory.BasicRole.GetAll());
        }

        public string Save(BasicRole model)
        {
            return ServiceFactory.BasicRole.Save(model).Serialize();
        }

        public string Remove(string id)
        {
            return ServiceFactory.BasicRole.Delete(id).Serialize();
        }

        public string SetOrder(string id, string order)
        {
            return ServiceFactory.BasicRole.Order(id, order).Serialize();
        }

        public string EnableOrDisable(string id, bool bit)
        {
            return ServiceFactory.BasicRole.EnableOrDisable(id, bit).Serialize();
        }

        public string SetPurview(string id, string data)
        {
            var list = JsonHelper.Deserialize<string[]>(data);

            return ServiceFactory.BasicRoleMenuFunction.SetMenuFunction(id, list).Serialize();
        }

        public string PurviewGrid(string id)
        {
            var menulist = ServiceFactory.BasicMenu.GetAll();
            var functionlist = ServiceFactory.BasicFunction.GetAll();
            var menufunctionlist = ServiceFactory.BasicMenuFunction.GetAll();
            var rolemenufunctionlist = ServiceFactory.BasicRoleMenuFunction.GetByRoleId(id);

            List<Hashtable> list = new List<Hashtable>();

            foreach (var menu in menulist)
            {
                Hashtable hash = new Hashtable();

                hash.Add("iconCls", menu.Icon);

                hash.Add("ID", menu.ID);
                hash.Add("Name", menu.Name);
                hash.Add("Pid", menu.Pid);

                List<Hashtable> purviewlist = new List<Hashtable>();

                foreach (var menufunction in menufunctionlist.Where(m => m.MenuID == menu.ID))
                {
                    Hashtable purviewhash = new Hashtable();

                    var function = functionlist.FirstOrDefault(m => m.ID == menufunction.FunctionID);

                    purviewhash.Add("ID", menufunction.ID);
                    purviewhash.Add("Icon", function.Icon);
                    purviewhash.Add("Name", function.Name);

                    purviewhash.Add("exist", rolemenufunctionlist.Any(m => m.MenuFunctionID == menufunction.ID));

                    purviewlist.Add(purviewhash);
                }

                hash.Add("PurviewList", purviewlist);

                list.Add(hash);
            }

            return JsonHelper.Serialize(list);
        }
    }
}
